package com.qq.mobilemanager.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qq.mobilesafe.R;
import com.qq.mobilesafe.base.adapter.ViewHolder;
import com.qq.mobilesafe.base.adapter.abslistview.CommonAdapter;
import com.qq.mobilesafe.db.dao.AppLockDao;
import com.qq.mobilesafe.domain.AppInfo;
import com.qq.mobilesafe.engine.AppInfoParser;
import com.qq.mobilesafe.manager.ThreadManager;

public class UnLockFragment extends Fragment {
	@ViewInject(R.id.tv_unlock)
	private TextView tv_unlock;

	@ViewInject(R.id.iv_unlock)
	private ImageView iv_unlock;

	@ViewInject(R.id.list_view)
	private ListView list_view;

	private UnLockAdapter adapter;

	private List<AppInfo> list;
	private List<AppInfo> unlockapp;
	private AppLockDao dao;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.item_unlock_fragment, null);
		list_view = (ListView) view.findViewById(R.id.list_view);

		tv_unlock = (TextView) view.findViewById(R.id.tv_unlock);

		ViewUtils.inject(getActivity());

		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		list = AppInfoParser.getAppInfos(getActivity());
		dao = new AppLockDao(getActivity());
		unlockapp = new ArrayList<AppInfo>();
		for (AppInfo appinfo : list) {

			if (TextUtils.isEmpty(dao.findLock(appinfo.packagename))) {
				if (appinfo.packagename.equals(
						getActivity().getPackageName())) {
					continue;
				}
				unlockapp.add(appinfo);
			}

		}

		adapter = new UnLockAdapter(getActivity(),R.layout.item_unlock, unlockapp);

		list_view.setAdapter(adapter);

	}

	private class UnLockAdapter extends CommonAdapter<AppInfo> {

		

		public UnLockAdapter(Context context, int layoutId, List<AppInfo> datas) {
			super(context, layoutId, datas);
		}

		@Override
		public int getCount() {
			tv_unlock.setText("未加锁(" + unlockapp.size() + ")个");
			return super.getCount();
		}

		@Override
		public void convert(final ViewHolder holder,
				final AppInfo appInfo) {

			holder.setText(R.id.tv_name, appInfo.name);
			holder.setImageDrawable(R.id.iv_icon, appInfo.icon);
			holder.setOnClickListener(R.id.iv_unlock, new OnClickListener() {

				@Override
				public void onClick(View v) {
					TranslateAnimation translateAnimation = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 1.0f,
							Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 0);
					// 设置动画时间
					translateAnimation.setDuration(200);
					// 开始动画
					holder.getConvertView().startAnimation(translateAnimation);
					ThreadManager.getInstance().createShortPool()
							.execute(new Runnable() {
								@Override
								public void run() {
									SystemClock.sleep(200);
									getActivity().runOnUiThread(new Runnable() {
										@Override
										public void run() {
											dao.add(appInfo.packagename);
											unlockapp.remove(holder.getPos());
											adapter.notifyDataSetChanged();
										}
									});
								}
							});
				}
			});
		}

	}

}
